/*
 * Copyright (c) 2021 Weasis Team and other contributors.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0, or the Apache
 * License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */
package org.weasis.dicom.qr;

import java.io.File;
import java.util.ResourceBundle;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.ElementDictionary;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.VR;
import org.dcm4che3.net.Status;

/**
 * ·Copyright·(c)·2021·Weasis·Team·and·other·contributors.
 * ·This·program·and·the·accompanying·materials·are·made·available·under·the·terms·of·the·Eclipse
 * ·Public·License·2.0·which·is·available·at·http://www.eclipse.org/legal/epl-2.0,·or·the·Apache
 * ·License,·Version·2.0·which·is·available·at·https://www.apache.org/licenses/LICENSE-2.0.
 * ·SPDX-License-Identifier:·EPL-2.0·OR·Apache-2.0
 */
public class TagHelper {
  static final ElementDictionary elementDict;

  static {
    elementDict = ElementDictionary.getStandardElementDictionary();
  }

  public static void printDicomDataset(Attributes attrs) {
    int[] tags = attrs.tags();
    for (int tag : tags) {
      VR vr = elementDict.vrOf(tag);
      String kw = elementDict.keywordOf(tag);
      String value = attrs.getString(tag);
      System.out.println(String.format("Tag:%32s,VR:%s,[%s]", kw, vr, value));
    }
  }

  public static int getIntOption(ResourceBundle rb, String key, int defVal) {

    try {

      return Integer.parseInt(rb.getString(key));
    } catch (NumberFormatException e) {
      return defVal;
    }
  }

  public static String getOptionValue(ResourceBundle rb, String key, String defVal) {

    if (rb.containsKey(key)) {
      return rb.getString(key);
    } else {
      return defVal;
    }
  }

  public static boolean getOptionValue(ResourceBundle rb, String key, boolean defVal) {

    if (rb.containsKey(key)) {
      try {
        return Boolean.parseBoolean(rb.getString(key));
      } catch (Exception ex) {
        return defVal;
      }
    } else {
      return defVal;
    }
  }

  public static void addTagAndValue(Attributes attrs, int[] tags, String[] values) {
    if (attrs == null) {
      return;
    }
    if (tags == null || values == null) {
      return;
    }
    if (tags.length != values.length) {
      return;
    }
    for (int i = 0; i < tags.length; i++) {
      int tag = tags[i];
      String val = values[i];
      VR vr = elementDict.vrOf(tag);
      attrs.setString(tag, vr, val);
    }
  }

  public static File mkStoreFilePath(
      final String storageDirectory,
      String patientUid,
      String studyUid,
      String seriesUid,
      String instUid) {
    File ds = new File(storageDirectory, patientUid);
    File studyFile = new File(ds, studyUid);
    File seriesFile = new File(studyFile, seriesUid);
    if (!seriesFile.exists()) {
      seriesFile.mkdirs();
    }
    String fn = String.format("%s.dcm", instUid);
    File imgFile = new File(seriesFile, fn);
    return imgFile;
  }

  public static Attributes refSOP(String iuid, String cuid, int failureReason) {
    Attributes attrs = new Attributes(3);
    attrs.setString(Tag.ReferencedSOPClassUID, VR.UI, cuid);
    attrs.setString(Tag.ReferencedSOPInstanceUID, VR.UI, iuid);
    if (failureReason != Status.Success) {
      attrs.setInt(Tag.FailureReason, VR.US, failureReason);
    }
    return attrs;
  }
}
